Dart List operator +
Syntax & Examples
Syntax of List.operator +
The syntax of List.operator + operator is:
operator +(List<E> other) → List<E>This operator + operator of List returns the concatenation of this list and other.
Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
other | required | the list to concatenate with this list |
✐ Examples
1 Concatenate integer lists
In this example,
- We create two lists named
list1andlist2containing integers. - We use the
+operator to concatenatelist1andlist2. - The resulting list contains elements from both lists in the order they were concatenated.
- We print the result to standard output.
Dart Program
void main() {
List<int> list1 = [1, 2, 3];
List<int> list2 = [4, 5, 6];
List<int> result = list1 + list2;
print(result);
}Output
[1, 2, 3, 4, 5, 6]
2 Concatenate string lists
In this example,
- We create two lists named
list1andlist2containing strings. - We use the
+operator to concatenatelist1andlist2. - The resulting list contains elements from both lists in the order they were concatenated.
- We print the result to standard output.
Dart Program
void main() {
List<String> list1 = ['a', 'b', 'c'];
List<String> list2 = ['x', 'y', 'z'];
List<String> result = list1 + list2;
print(result);
}Output
[a, b, c, x, y, z]
3 Concatenate double lists
In this example,
- We create two lists named
list1andlist2containing double values. - We use the
+operator to concatenatelist1andlist2. - The resulting list contains elements from both lists in the order they were concatenated.
- We print the result to standard output.
Dart Program
void main() {
List<double> list1 = [1.1, 2.2, 3.3];
List<double> list2 = [4.4, 5.5, 6.6];
List<double> result = list1 + list2;
print(result);
}Output
[1.1, 2.2, 3.3, 4.4, 5.5, 6.6]
Summary
In this Dart tutorial, we learned about operator + operator of List: the syntax and few working examples with output and detailed explanation for each example.